package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.GambitBean;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.GambitEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.*;

/**
 * @author 王进 on 2019/3/26.
 */
//我的收藏
public class CollectLogic {
    private Context context;
    private GambitListBean bean;
    private LogicCallBack callBack;
    public CollectLogic(Activity activity,GambitListBean bean,LogicCallBack callBack){
        this.context = activity;
        this.callBack = callBack;
        this.bean = bean;
    }

    private int pageIndex;
    public void refresh(){
        pageIndex = 1;
        loadData(pageIndex);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(pageIndex);
    }
    private void loadData(final int pageIndex){
        JSONObject json = new JSONObject();
        json.put("pageIndex",pageIndex);
        json.put("pageSize",10);
        httpCallForm(BBSConfig.listCollection,json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onNoResponse(int code) {
                callBack.onResult(false);
            }
            @Override
            public void onError(Exception e) {
                if (callBack==null)return;
                callBack.onResult(false);
            }

            @Override
            public void onResponse(JsonParse parse) {
                if (callBack==null)return;
                TypeReference<PageEntity<GambitEntity>> reference = new TypeReference<PageEntity<GambitEntity>>() {
                };
                PageEntity<GambitEntity> response = parse.parsePageEntity(reference);
                List<GambitEntity> data = response.getData();
                ArrayList<GambitBean> list = new ArrayList<>();
                for (GambitEntity entity : data) {
                    GambitBean bean = new GambitBean();
                    //组装 话题
                    TopicBean topic = new TopicBean();
                    String topicTitle = entity.getTopicTitle();
                    topic.setTopicTitle(topicTitle);
                    topic.setTopicCode(entity.getTopicCode());
                    topic.setReviewCount(entity.getReviewNum());
                    bean.setTopic(topic);


                    InformationBean info = new InformationBean();
                    //region 组装回答用户
                    BBSUserBean user = new BBSUserBean();
                    user.setUserPin(entity.getUserPin()); //回答用户PIN
                    user.setUserName(entity.getUserName());//回答用户姓名
                    user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl())); //回答用户头像
                    if (!TextUtils.isEmpty(entity.getAnswerStoreName())){
                        user.setUserAddress(entity.getAnswerStoreName()); //回答用户门店
                    }else {
                        user.setUserAddress(entity.getUserTitleLevel());
                    }
                    info.setUser(user);
                    //endregion
                    int contentType = entity.getContentType();
                    if (contentType==1){
                        info.setMessageContent(entity.getContent()); //回答内容
                    }else {
                        info.setSoundContent(SoundBean.create(entity.getContent())); //回答内容
                    }
                    info.setAnswerCode(entity.getAnswerCode());
                    info.setReviewCount(entity.getReviewNum()); //回答阅读次数
                    info.setLikeCount(entity.getSupportCount()); //回答点赞次数
                    info.setOwnLike(entity.getIsUser() == 1); //是否自己已经点赞
                    bean.setInfo(info);

                    list.add(bean);
                }
                if (pageIndex==1){
                    bean.setGambitBeans(list);
                }else {
                    bean.getGambitBeans().addAll(list);
                }
                boolean pageAvailable = response.getNextPageAvailable();
                callBack.onResult(pageAvailable);
            }
        }));
    }

    public interface LogicCallBack{

        void onResult(boolean hasNextPage);
    }
}
